package com.xuecheng.learning.feignclient;

import com.xuecheng.content.model.po.CoursePublish;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Mr.M
 * @version 1.0
 * @description 内容管理远程接口
 * @date 2022/10/25 9:13
 */
@FeignClient(value = "content-api", fallbackFactory = ContentServiceClient.ContentServiceClientFallbackFactory.class)
public interface ContentServiceClient {

    @ResponseBody
    @GetMapping("/content/r/coursepublish/{courseId}")
    public CoursePublish getCoursepublish(@PathVariable("courseId") Long courseId);

    @Slf4j
    @Component
    public class ContentServiceClientFallbackFactory implements FallbackFactory<ContentServiceClient> {
        @Override
        public ContentServiceClient create(Throwable throwable) {
            return new ContentServiceClient() {

                @Override
                public CoursePublish getCoursepublish(Long courseId) {
                    log.error("调用内容管理服务发生熔断:{}", throwable.getMessage());
                    return null;
                }
            };
        }
    }

}
